(function () {
    var basrUrl = "http://127.0.0.1:11209?";
    var URL = {
        TREE: basrUrl + "method=getTree" + "&path=",
        ROOT_PATH: basrUrl + "method=rootPath",
        SAVE_HISTORY: basrUrl + "method=saveHistory"
    };
    try {
        var vue = new Vue({
            el: "#app",
            data: function () {
                return {
                    filterText: '',
                    treeData: [],
                    defaultProps: {
                        children: 'children',
                        label: 'label',
                        isLeaf: 'isLeaf'
                    },
                    textarea: '',
                    winCodePath: '',
                    linuxCodePath: '',
                    oaRootPath: '请选择项目根目录',
                    hrRootPath: '请选择项目根目录',
                    sys: 'oa',
                    requestMsg: '',
                    first: true
                };
            },
            watch: {
                filterText: function (val) {
                    if (val) {
                        val = val.trim();
                    }
                    this.$refs.fileTree.filter(val);
                },
                sys: function (newValue, oldValue) {
                    this.textarea = '';
                    this.sysChange(newValue);
                }
            },
            methods: {
                filterNode: function (value, data) {
                    if (!value) return true;
                    return data.label.indexOf(value) !== -1;
                },
                loadChildren: function (node, resolve) {
                    try{
                        var that = this;
                        var path;
                        if (node.level === 0) {
                            path = 'root';
                        } else {
                            path = node.data.path;
                        }
                        if (that.first){
                            resolve([]);
                        }else {
                            var obj = window.fileOpt.getTree(path, that.sys);
                            if (obj){
                                obj = JSON.parse(obj);
                                if (obj.data.status === '1') {
                                    if (obj.data.data) {
                                        resolve(obj.data.data);
                                    }
                                } else {
                                    that.$message(obj.data.msg);
                                }
                            } else {
                                $.get(URL.TREE+path+"&sys="+that.sys,function (response) {
                                    resolve(response.data.data);
                                });
                            }

                        }
                    }catch (error){
                        document.getElementById("msg").innerHTML = error.message;
                    }
                },
                nodeCheck: function (node, status) {
                    var checkedNodes = status.checkedNodes;
                    var winText = "";
                    var linuxText = "";
                    for (var i = 0; i < checkedNodes.length; i++) {
                        winText += checkedNodes[i].winPath + "</br>";
                        linuxText += checkedNodes[i].linuxPath + "</br>";
                    }
                    this.winCodePath = winText;
                    this.linuxCodePath = linuxText;
                },
                choseRoot: function (sys) {
                    var that = this;
                    try {
                        if (sys === 'oa') {
                            var path = window.fileOpt.choseRootDirPath(sys);
                            this.oaRootPath = path;
                            that.sysChange();
                        } else {
                            this.hrRootPath = window.fileOpt.choseRootDirPath(sys);
                            that.sysChange();
                        }
                    } catch (e) {
                        that.$message("设置路径出错:" + e.message);
                    }
                },
                sysChange: function () {
                    var that = this;

                    var obj = window.fileOpt.getTree('root', that.sys);
                    obj = JSON.parse(obj);
                    if (obj.data.status === '1') {
                        if (obj.data.data) {
                            that.treeData = obj.data.data;
                            that.$message({
                                message: "加载成功",
                                type: 'success'
                            });
                        }
                    } else {
                        that.$message(obj.data.msg);
                    }
                },
                saveHistory: function () {
                    var that = this;
                    try{
                        var obj = window.fileOpt.saveHistory(that.winCodePath,that.linuxCodePath,that.sys);
                        if (obj){
                            obj = JSON.parse(obj);
                            if (obj.data.status === '1') {
                                that.$message({
                                    message: "保存成功",
                                    type: 'success'
                                });
                            } else {
                                that.$message(obj.data.msg);
                            }
                        } else {
                            $.get(URL.SAVE_HISTORY+"&winCodePath="+that.winCodePath+"&linuxCodePath="+that.linuxCodePath+"&sys="+that.sys,function (response) {
                                if (response.data.status === '1') {
                                    that.$message({
                                        message: "保存成功咯",
                                        type: 'success'
                                    });
                                } else {
                                    that.$message(response.data.msg);
                                }
                            });
                        }

                    }catch (error){
                        that.$message(error.message);
                    }
                }
            },
            created: function () {
                var that = this;
                setTimeout(function () {
                    try {
                        var obj = window.fileOpt.getRootPath();
                        obj = JSON.parse(obj);
                        that.oaRootPath = obj.data.data.oaRootPath;
                        that.hrRootPath = obj.data.data.hrRootPath;

                        that.sysChange();
                        that.first = false;
                    } catch (error) {
                        document.getElementById("msg").innerHTML = error.message;
                    }
                }, 1000);
            }
        });
    } catch (error) {
        document.getElementById("msg").innerHTML = error.message;
    }

})();